package cn.iocoder.yudao.module.scrm.dal.mysql.business;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.scrm.controller.admin.business.vo.business.BusinessPageReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.business.vo.business.BusinessRespVO;
import cn.iocoder.yudao.module.scrm.controller.admin.clue.vo.CluePageReqVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.business.BusinessDO;

import cn.iocoder.yudao.module.scrm.dal.dataobject.clue.ClueDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.customer.CustomerDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.util.StringUtils;


/**
 * 商机 Mapper
 *
 * @author admin
 */
@Mapper
public interface ScrmBusinessMapper extends BaseMapperX<BusinessDO> {

   /* default PageResult<BusinessDO> selectPage(BusinessPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<BusinessDO>()
                .eqIfPresent(BusinessDO::getTitle, reqVO.getTitle())
                .eqIfPresent(BusinessDO::getCustomerId, reqVO.getCustomerId())
                .eqIfPresent(BusinessDO::getOrderId, reqVO.getOrderId())
                .eqIfPresent(BusinessDO::getContacts, reqVO.getContacts())
                .eqIfPresent(BusinessDO::getPredictMarketPrice, reqVO.getPredictMarketPrice())
                .betweenIfPresent(BusinessDO::getPredictWrittenTime, reqVO.getPredictWrittenTime())
                .eqIfPresent(BusinessDO::getSalesStage, reqVO.getSalesStage())
                .eqIfPresent(BusinessDO::getBusinessType, reqVO.getBusinessType())
                .eqIfPresent(BusinessDO::getTransactionProbability, reqVO.getTransactionProbability())
                .eqIfPresent(BusinessDO::getBusinessSource, reqVO.getBusinessSource())
                .betweenIfPresent(BusinessDO::getAcquisitionDate, reqVO.getAcquisitionDate())
                .eqIfPresent(BusinessDO::getVestingOfficer, reqVO.getVestingOfficer())
                .eqIfPresent(BusinessDO::getRemark, reqVO.getRemark())
                .eqIfPresent(BusinessDO::getSystemNumber, reqVO.getSystemNumber())
                .eqIfPresent(BusinessDO::getOwnerUserId, reqVO.getOwnerUserId())
                .eqIfPresent(BusinessDO::getOldOwnerUserId, reqVO.getOldOwnerUserId())
                .betweenIfPresent(BusinessDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(BusinessDO::getId));
    }*/

    default PageResult<BusinessDO> selectPage(BusinessPageReqVO reqVO) {
        // 假设 reqVO.getPredictWrittenTime() 返回要查询的日期
        LocalDateTime startOfDay = (reqVO.getPredictWrittenTime() != null && reqVO.getPredictWrittenTime().length > 0) ?
                reqVO.getPredictWrittenTime()[0].toLocalDate().atStartOfDay() : null;
        LocalDateTime endOfDay = (reqVO.getPredictWrittenTime() != null && reqVO.getPredictWrittenTime().length > 0) ?
                reqVO.getPredictWrittenTime()[0].toLocalDate().atTime(LocalTime.MAX) : null;


        return selectPage(reqVO, new LambdaQueryWrapperX<BusinessDO>()
                .like(StringUtils.hasText(reqVO.getKey()), BusinessDO::getTitle, reqVO.getKey())
                .or().like(StringUtils.hasText(reqVO.getKey()), BusinessDO::getRemark, reqVO.getKey())
                .or().like(StringUtils.hasText(reqVO.getKey()), BusinessDO::getSystemNumber, reqVO.getKey())

                .eq(StringUtils.hasText(reqVO.getSalesStage()), BusinessDO::getSalesStage, reqVO.getSalesStage())
                .eq(StringUtils.hasText(reqVO.getBusinessSource()), BusinessDO::getBusinessSource, reqVO.getBusinessSource())
                .eq(StringUtils.hasText(reqVO.getTransactionProbability()), BusinessDO::getTransactionProbability, reqVO.getTransactionProbability())
                .between(reqVO.getPredictWrittenTime() != null && reqVO.getPredictWrittenTimeEnd() != null, BusinessDO::getPredictWrittenTime, reqVO.getPredictWrittenTime(), reqVO.getPredictWrittenTimeEnd())
                .between(startOfDay != null, BusinessDO::getPredictWrittenTime, startOfDay, endOfDay)
                .in(reqVO.getUserIds() != null && !reqVO.getUserIds().isEmpty(), BusinessDO::getVestingOfficer, reqVO.getUserIds())
                .orderByDesc(BusinessDO::getCreateTime));
    }



    default PageResult<BusinessRespVO> selectPages(BusinessPageReqVO reqVO) {
        // 假设 reqVO.getPredictWrittenTime() 返回要查询的日期
        LocalDateTime startOfDay = (reqVO.getPredictWrittenTime() != null && reqVO.getPredictWrittenTime().length > 0) ?
                reqVO.getPredictWrittenTime()[0].toLocalDate().atStartOfDay() : null;
        LocalDateTime endOfDay = (reqVO.getPredictWrittenTime() != null && reqVO.getPredictWrittenTime().length > 0) ?
                reqVO.getPredictWrittenTime()[0].toLocalDate().atTime(LocalTime.MAX) : null;

        MPJLambdaWrapper<BusinessDO> query = new MPJLambdaWrapper<BusinessDO>()
                .selectAll(BusinessDO.class)//查询user表全部字段
                .selectAs(CustomerDO::getName, BusinessRespVO::getCustomerIdName)
                .like(StringUtils.hasText(reqVO.getKey()), BusinessDO::getTitle, reqVO.getKey())
                .or().like(StringUtils.hasText(reqVO.getKey()), BusinessDO::getRemark, reqVO.getKey())
                .or().like(StringUtils.hasText(reqVO.getKey()), BusinessDO::getSystemNumber, reqVO.getKey())

                .eq(StringUtils.hasText(reqVO.getSalesStage()), BusinessDO::getSalesStage, reqVO.getSalesStage())
                .eq(StringUtils.hasText(reqVO.getBusinessSource()), BusinessDO::getBusinessSource, reqVO.getBusinessSource())
                .eq(StringUtils.hasText(reqVO.getTransactionProbability()), BusinessDO::getTransactionProbability, reqVO.getTransactionProbability())
                .between(reqVO.getPredictWrittenTime() != null && reqVO.getPredictWrittenTimeEnd() != null, BusinessDO::getPredictWrittenTime, reqVO.getPredictWrittenTime(), reqVO.getPredictWrittenTimeEnd())
                .between(startOfDay != null, BusinessDO::getPredictWrittenTime, startOfDay, endOfDay)
                .in(reqVO.getUserIds() != null && !reqVO.getUserIds().isEmpty(), BusinessDO::getVestingOfficer, reqVO.getUserIds())
                .ge(reqVO.getSessionBeginTime() != null, BusinessDO::getSessionBeginTime, reqVO.getSessionBeginTime())
                .le(reqVO.getSessionEndTime() != null, BusinessDO::getSessionEndTime, reqVO.getSessionEndTime())

                .ge(reqVO.getAcquisitionDate() != null, BusinessDO::getAcquisitionDate, reqVO.getAcquisitionDate())
                .le(reqVO.getAcquisitionDateEnd() != null, BusinessDO::getAcquisitionDate, reqVO.getAcquisitionDateEnd())

                .eq(StringUtils.hasLength(reqVO.getSignCondition()), BusinessDO::getSignCondition, reqVO.getSignCondition())
                .orderByDesc(BusinessDO::getCreateTime);

                 query.leftJoin(CustomerDO.class,CustomerDO ::getId, BusinessDO::getCustomerId)
                  .like(reqVO.getCustomerIdName() != null, CustomerDO::getName, reqVO.getCustomerIdName());

                    //.groupBy(CustomerDO::getId); // 避免 1 对多查询，产生相同的 1

        return selectJoinPage(reqVO, BusinessRespVO.class, query);
    }

}